package kliment.net {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.Responder;

	import kliment.events.AMFResponderEvent;

	[Event(name = 'complete', type = 'kliment.events.AMFResponderEvent')]
	[Event(name = 'error', type = 'kliment.events.AMFResponderEvent')]

	/**
	* ...
	* @author	Kliment
	* @version	1.1.2
	*/
	public class AMFResponder extends Responder implements IEventDispatcher {
		private var _eventDispatcher:EventDispatcher;
		private var _result:Object;
		private var _status:Object;

		public var parameters:Array;

		public function AMFResponder() {
			super(_resultHandler, _statusHandler);
			_eventDispatcher = new EventDispatcher();
		}

		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
			_eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}

		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
			_eventDispatcher.removeEventListener(type, listener, useCapture)
		}

		public function hasEventListener(type:String):Boolean {
			return _eventDispatcher.hasEventListener(type);
		}

		public function get result():Object { return _result; }

		public function get status():Object { return _status; }

		private function _resultHandler(result:Object):void {
			_result = result;
			_eventDispatcher.dispatchEvent(new AMFResponderEvent(AMFResponderEvent.COMPLETE, this, _result, _status));
		}

		private function _statusHandler(status:Object):void {
			_status = status;
			_eventDispatcher.dispatchEvent(new AMFResponderEvent(AMFResponderEvent.ERROR, this, _result, _status));
		}

		public function dispatchEvent(event:Event):Boolean
		{
			throw new Error("Don't use this method");
			return false;
		}

		public function willTrigger(type:String):Boolean
		{
			throw new Error("Don't use this method");
			return false;
		}
	}
}